package com.angenela.imageshare.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.angenela.imageshare.MainActivity;
import com.angenela.imageshare.R;
import com.angenela.imageshare.dto.UserDTO;
import com.angenela.imageshare.pojo.User;
import com.angenela.imageshare.util.JsonUtil;

import java.io.IOException;

import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegistryActivity extends AppCompatActivity {

    private static final String REGISTRY_URL = "http://122.9.152.182:8080/user/register";

    private EditText username;
    private EditText password;
    private EditText checkPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registry);

        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        checkPassword = findViewById(R.id.checkPassword);
    }

    public void registry(View view) throws IOException {
        String passwordVal = this.password.getText().toString();
        String checkPasswordVal = this.checkPassword.getText().toString();
        String usernameVal = username.getText().toString();

        // 判断两次密码是否一致
        if (!passwordVal.equals(checkPasswordVal)) {
            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
            dialogBuilder.setMessage("两次输入的密码不一致");
            dialogBuilder.setTitle("提示");
            dialogBuilder.setPositiveButton("确定", null);
            AlertDialog alertdialog = dialogBuilder.create();
            alertdialog.show();
            return;
        }

        // 向后端传递数据
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("username", usernameVal)
                .addFormDataPart("password", passwordVal)
                .build();
        Request request = new Request.Builder()
                .url(REGISTRY_URL)
                .post(requestBody)
                .build();
        Response response = okHttpClient.newCall(request).execute();
        UserDTO userDTO = JsonUtil.jsonToObject(UserDTO.class, response.body().string());

        // 判断用户是否存在
        if (!userDTO.getCode().equals(200)) {
            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
            dialogBuilder.setMessage("用户已存在");
            dialogBuilder.setTitle("提示");
            dialogBuilder.setPositiveButton("确定", null);
            AlertDialog alertdialog = dialogBuilder.create();
            alertdialog.show();
            return;
        }

        // 保存用户信息
        Resources resources = getResources();
        String spFileNameKey = resources.getString(R.string.shared_preferences_file_name);
        String usernameKey = resources.getString(R.string.login_account_username);
        String idKey = resources.getString(R.string.login_account_id);
        String avatarKey = resources.getString(R.string.login_account_avatar);
        String createTimeKey = resources.getString(R.string.login_account_create_time);
        String passwordKey = resources.getString(R.string.login_account_password);

        SharedPreferences sharedPreferences =
                getSharedPreferences(spFileNameKey, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        User user = userDTO.getData();

        editor.putString(usernameKey, user.getUsername());
        editor.putString(passwordKey, user.getPassword());
        editor.putInt(idKey, user.getId());
        editor.putString(createTimeKey, user.getCreateTime());
        editor.putString(avatarKey, user.getAvatar());

        // 转到MainActivity
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}
